package com.cya.lotterysystem.service.activityStatus.operator;

import com.cya.lotterysystem.dao.mapper.ActivityUserMapper;
import com.cya.lotterysystem.service.dto.ActivityUserDO;
import com.cya.lotterysystem.service.dto.ConverActivityStatusDTO;
import com.cya.lotterysystem.service.enums.ActivityStatusEnum;
import com.cya.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/11/7 11:57
 * @description：
 * @modified By：
 * @version:
 */
@Component
public class UserOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityUserMapper activityUserMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConverActivityStatusDTO converActivityStatusDTO) {

        Long activityId=converActivityStatusDTO.getActivityId();
        List<Long> userIds=converActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetStatus=converActivityStatusDTO.getTargetUserStatus();
        if (null==activityId||CollectionUtils.isEmpty(userIds)||null==targetStatus){
            return false;
        }

        List<ActivityUserDO> activityUserDOList = activityUserMapper.batchSelectByAUIds(activityId, userIds);
        if (CollectionUtils.isEmpty(activityUserDOList)){
            return false;
        }
        for (ActivityUserDO auDO:activityUserDOList){
           {
               if ( auDO.getStatus().equalsIgnoreCase(targetStatus.name())){
                   return false;

               }
            }

        }


        return true;
    }

    /**
     * 转换方法
     * @param converActivityStatusDTO
     * @return
     */
    @Override
    public Boolean convert(ConverActivityStatusDTO converActivityStatusDTO) {
        Long activityId=converActivityStatusDTO.getActivityId();
        List<Long> userIds=converActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetStatus=converActivityStatusDTO.getTargetUserStatus();
        try {
            activityUserMapper.batchUpdateStatus(activityId,userIds,targetStatus.name());
            return true;
        }catch (Exception e){
            return false;

        }
    }
}